const fs = require('fs');
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true, useUnifiedTopology: true});

const Schema = mongoose.Schema;

const channelSchema = new Schema({
    channelId: String,
    name: String
});
const newsSchema = new Schema({
    id: String,
    channelId: String,
    pubDate: Date,
    channelName: String,
    desc: String,
    link: String,
    content: String,
    havePic: Boolean,
    title: String,
    imageurls: Array,
    source: String
});

let Channels = mongoose.model('Channel', channelSchema);
let News = mongoose.model('new', newsSchema);

async function saveChannels() {
    fs.readFile('./channels.json', 'utf8', function(err, data) {
        let channels = JSON.parse(data);
        Channels.insertMany(channels, function(err, docs) {
            if (err) {
                console.log('err', err);
            } 
            console.log('docs', docs);
        });
    });
}

async function saveNews() {
    fs.readFile('./news.json', 'utf8', function(err, data) {
        let news = JSON.parse(data);
        console.log(news);
        News.insertMany(news.contentlist, function(err, docs) {
            if (err) {
                console.log('err', err);
            } 
            console.log('docs', docs);
        });
    });
}

async function findAll() {
    let ret = await Channels.find();
    console.log(ret);
}
